/* SelectionView.cpp
 * View that displays the available Ninjas and their characteristics
 *
 * Last Modified: 16.01.2014
 * Author: Guilherme S.G.
 *
 * TODO: Exhibit the list of jutsus (in an interactive form with explanation) for each ninja
 */
#include "SelectionView.h"
#include "Game.h"


SelectionView::SelectionView() : View()
{
    ResourceManager *resources = controller->getResources();
    ALLEGRO_BITMAP *backgroundNaruto, *backgroundSasuke;
    Ninja *naruto;
    Ninja *sasuke;
    
    backgroundNaruto = resources->loadImage("assets/backgrounds/naruto-bg-1.jpg", "naruto-bg");
    backgroundSasuke = resources->loadImage("assets/backgrounds/sasuke-bg-1.png", "sasuke-bg");
    
    backgrounds.push_back(backgroundNaruto);
    backgrounds.push_back(backgroundSasuke);
    
    selectionCursor = new Sprite("cursor");
    selectionCursor->setImage(resources->loadImage("assets/misc/selection-cursor.png", "selection-cursor"));
    
    fontNJ = resources->loadFont("assets/fonts/njnaruto.ttf", 12, 0, "font-naruto");
    fontDescr = resources->loadFont("assets/fonts/Arial.ttf", 14, 0, "arial-20");
    
    bgMusic = resources->loadSound("assets/audio/ost1-character-selection.ogg", "bg-music");
    selectionFx = resources->loadSound("assets/audio/fast_cut.ogg", "selectionFx");
    
    naruto = Ninja::loadChar("assets/characters/naruto.char");
    naruto->setPosition(20, 100);
    controller->addNinja(naruto);
    
    sasuke = Ninja::loadChar("assets/characters/sasuke.char");
    sasuke->setPosition(20, 225);
    controller->addNinja(sasuke);
    
    ninjas.push_back(naruto);
    ninjas.push_back(sasuke);
    
    selectionID = 0;
    al_play_sample(bgMusic, 1, 0, 1, ALLEGRO_PLAYMODE_LOOP, &bgMusicID);
}

SelectionView::~SelectionView()
{
    al_stop_sample(&bgMusicID);
}

void SelectionView::handleEvents(ALLEGRO_EVENT *event)
{
    if(event->type == ALLEGRO_EVENT_KEY_UP)
    {
        switch (event->keyboard.keycode)
        {
            case ALLEGRO_KEY_ESCAPE:
                controller->goToPreviousView();
                break;
                
            case ALLEGRO_KEY_ENTER:
                controller->setNextView(VERSUS);
                controller->setPlayerSelection(selectionID);
                break;
        }
    }
    else if(event->type == ALLEGRO_EVENT_KEY_DOWN)
    {
        switch (event->keyboard.keycode)
        {
            case ALLEGRO_KEY_UP:
                selectionID--;
                break;
            case ALLEGRO_KEY_DOWN:
                selectionID++;
                break;
        }
        al_play_sample(selectionFx, 1, 0, 1, ALLEGRO_PLAYMODE_ONCE, 0);
    }
}

void SelectionView::update()
{
    if(selectionID < 0)
        selectionID = 0;
    else if(selectionID > ninjas.size()-1)
        selectionID = (int)ninjas.size()-1;
    
    selectionCursor->setPosition(ninjas[selectionID]->getPosX(), ninjas[selectionID]->getPosY());
}

void SelectionView::render()
{
    al_draw_bitmap(backgrounds[selectionID], 0, 0, 0);
    
    selectionCursor->render(nullptr, 0);
    for(int i = 0; i < ninjas.size(); i++)
    {
        Ninja *ninja = ninjas[selectionID];
        Sprite *portrait = ninja->getPortrait();
        Frame frameClip = portrait->getFrame(0);
        
        portrait->render(&frameClip, 0);
        if(i == selectionID)
        {
            int posX = ninja->getPosX();
            int posY = ninja->getPosY();
            int offsetX = selectionCursor->getWidth()+15;
            
            al_draw_text(fontNJ, al_map_rgb(255, 128, 0), posX+offsetX+60, posY+5, ALLEGRO_ALIGN_RIGHT, "Name:");
            al_draw_text(fontNJ, al_map_rgb(255, 128, 0), posX+offsetX+60, posY+40, ALLEGRO_ALIGN_RIGHT, "Element:");
            al_draw_text(fontNJ, al_map_rgb(255, 128, 0), posX+offsetX+60, posY+75, ALLEGRO_ALIGN_RIGHT, "Family:");
            al_draw_text(fontNJ, al_map_rgb(255, 128, 0), posX+offsetX+60, posY+110, ALLEGRO_ALIGN_RIGHT, "Description:");
            
            al_draw_text(fontDescr, al_map_rgb(255, 255, 255), posX+offsetX+70, posY+5, ALLEGRO_ALIGN_LEFT, ninja->getName().c_str());
            al_draw_text(fontDescr, al_map_rgb(255, 255, 255), posX+offsetX+70, posY+40, ALLEGRO_ALIGN_LEFT, ninja->getElement().c_str());
            al_draw_text(fontDescr, al_map_rgb(255, 255, 255), posX+offsetX+70, posY+75, ALLEGRO_ALIGN_LEFT, ninja->getFamily().c_str());
            al_draw_text(fontDescr, al_map_rgb(255, 255, 255), posX+offsetX+70, posY+110, ALLEGRO_ALIGN_LEFT, ninja->getDescription().c_str());
        }
    }
    
}
